//-----------------------------------------------------------------------------
//  
//  Copyright (c) 2013
//
//  Project  : 
//  Module   : 
//  Parent   : 
//
//  Description: 
//
//
//  Notes       : 
//


module STSC_TOP(
   input                  GTM_RESET,
   input                  GTM_CLK155M,

   input                  XC_0_FP,
   input                  XC_1_FP,

   output                 GTM_AUPP_FP8K,
   output                 GTM_BKP_FP8K
   );





reg[3:0]                               TMFP_XC0_SREGS, TMFP_XC1_SREGS;
reg                                    TMFP_XC0_FPEN, TMFP_XC1_FPEN;

reg[15:0]                              TMFP_XC0_FILTER_CNT;
reg                                    TMFP_XC0_FP;


// input FP filter and switch
always @( posedge GTM_RESET or posedge GTM_CLK155M ) begin
   if (GTM_RESET==1'b1) begin
      TMFP_XC0_SREGS[3:0]                                <= 4'd0;
      TMFP_XC1_SREGS[3:0]                                <= 4'd0;
   end
   else begin
      TMFP_XC0_SREGS[3:0]                                <= {TMFP_XC0_SREGS[2:0], XC_0_FP};
      TMFP_XC1_SREGS[3:0]                                <= {TMFP_XC1_SREGS[2:0], XC_1_FP};
   end
end

always @( posedge GTM_RESET or posedge GTM_CLK155M ) begin
   if (GTM_RESET==1'b1) begin
      TMFP_XC0_FPEN                                      <= 1'b0;
      TMFP_XC1_FPEN                                      <= 1'b0;
   end
   else begin
      TMFP_XC0_FPEN                                      <= TMFP_XC0_SREGS[3]==1'b0 && TMFP_XC0_SREGS[2]==1'b1;
      TMFP_XC1_FPEN                                      <= TMFP_XC1_SREGS[3]==1'b0 && TMFP_XC1_SREGS[2]==1'b1;
   end
end

always @( posedge GTM_RESET or posedge GTM_CLK155M ) begin
   if (GTM_RESET==1'b1) begin
      TMFP_XC0_FP                                        <= 1'b0;
      TMFP_XC0_FILTER_CNT[15:0]                          <= 16'd0;
   end
   else begin
      TMFP_XC0_FP                                        <= (TMFP_XC0_FILTER_CNT[15:0]==16'd19439);
      if ( TMFP_XC0_FPEN==1'b1 ) begin
         if ( TMFP_XC0_FILTER_CNT[15:0]!=16'd19437 && TMFP_XC0_FILTER_CNT[15:0]!=16'd19438 && TMFP_XC0_FILTER_CNT[15:0]!=16'd19439 && TMFP_XC0_FILTER_CNT[15:0]!=16'd0 && TMFP_XC0_FILTER_CNT[15:0]!=16'd1 && TMFP_XC0_FILTER_CNT[15:0]!=16'd2)
            TMFP_XC0_FILTER_CNT[15:0]                    <= 16'd0;
         else begin
            if (TMFP_XC0_FILTER_CNT[15:0]==16'd19439)
               TMFP_XC0_FILTER_CNT[15:0]                 <= 16'd0;
            else
               TMFP_XC0_FILTER_CNT[15:0]                 <= TMFP_XC0_FILTER_CNT[15:0] +16'd1;
         end
      end
      else begin
         if (TMFP_XC0_FILTER_CNT[15:0]==16'd19439)
            TMFP_XC0_FILTER_CNT[15:0]                    <= 16'd0;
         else
            TMFP_XC0_FILTER_CNT[15:0]                    <= TMFP_XC0_FILTER_CNT[15:0] +16'd1;
      end
   end
end
  assign  GTM_AUPP_FP8K   = TMFP_XC0_FP;
  assign  GTM_BKP_FP8K    = TMFP_XC0_FILTER_CNT[15:0]==16'd600;


endmodule
